//package medicalApp.velociraptors.com;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
* The UserLookupResults class is designed to aid in looking up users from the database.
*
* @author Vishnu, lsun
*/ 
@SuppressWarnings("serial")
public class UserLookupResults extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private String firstName;
	private String lastName;
	private int socialSN;

	private MedAppController mac;
	private JTable table;
	protected Integer chosenSSN;
	private JButton btnDeleteUser;
	private JButton btnCancel;

	/**
	 * Create the dialog.
	 * 
	 * @param mac The MedAppController object that drives this class.
	 * @param drSSN The Doctor's SSN
	 */
	public UserLookupResults(final MedAppController mac, final int drSSN) {
		this.mac = mac;
		setBounds(100, 100, 800, 600);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 11, 770, 491);
			contentPanel.add(scrollPane);
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				/**
				 * Designed to perform the action specified upon triggering by mouse click.
				 * 
				 * @param arg0 The MouseEvent that triggers the method call.
				 */ 
				public void mouseClicked(MouseEvent arg0) {
					if (arg0.getClickCount() == 2) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 2);
							UserView dpv = new UserView(mac, chosenSSN,drSSN);
							dpv.setVisible(true);
						}catch(Exception e){}
					}
					if (arg0.getClickCount() == 1) {
						JTable target = (JTable)arg0.getSource();
						int row = target.getSelectedRow();
						try{
							chosenSSN = (Integer) target.getValueAt(row, 2);
						}catch(Exception e){}
					}
				}
			});
			table.setFillsViewportHeight(true);
			scrollPane.setViewportView(table);
		}

		btnDeleteUser = new JButton("Delete Selected User");
		btnDeleteUser.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				if(chosenSSN!=null){
					int dialog = JOptionPane.showConfirmDialog(null, "Do you want to delete this user?");
					if(dialog == JOptionPane.YES_OPTION){

						try {
							String sql2 = "DELETE FROM `user` WHERE U_SSN = '" + chosenSSN + "'";
							Connection con = mac.getCon();
							Statement stmt = con.createStatement();
							int rs2 = stmt.executeUpdate(sql2);
							JOptionPane.showMessageDialog(null, "User Deleted!");
							mac.setDoctorPatientTableModel();
							table.setModel(myModel());
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				else
					JOptionPane.showMessageDialog(null, "Select a User!");
			}
		});
		btnDeleteUser.setBounds(20, 513, 180, 30);
		contentPanel.add(btnDeleteUser);

		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			/**
			 * Designed to perform the action specified upon triggering.
			 * 
			 * @param arg0 The ActionEvent that triggers the method call.
			 */ 
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancel.setBounds(600, 513, 180, 30);
		contentPanel.add(btnCancel);
	}

	/**
	 * Establishes the TableModel for use in this class.
	 */ 
	public LookupTableModelAdmin myModel(){
		mac.out.println("LookupModel" + firstName + " " + lastName + " " + socialSN);
		LookupTableModelAdmin model = new LookupTableModelAdmin(mac,firstName,lastName,socialSN);
		return model;
	}

	/**
	 * Serves as a setter for the TableModel
	 *
	 * @param model The LookupTableModelAdmin object that will be set.
	 */ 
	public void setTableModel(LookupTableModelAdmin model){
		table.setModel(model);
	}

	/**
	 * Getter for FirstName
	 * 
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * Setter for FirstName
	 * 
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * Getter for LastName
	 * 
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * Setter for LastName
	 * 
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * Getter for socialSN
	 * 
	 * @return the socialSN
	 */
	public int getSocialSN() {
		return socialSN;
	}

	/**
	 * Setter for socialSN
	 * 
	 * @param socialSN the socialSN to set
	 */
	public void setSocialSN(int socialSN) {
		this.socialSN = socialSN;
	}
}
